package com.sgb.kjwl.view.ui.fragment.manage;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;

import com.bumptech.glide.Glide;
import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FrPromotionalAlbumForWebsiteBinding;
import com.sgb.kjwl.di.manage.DaggerEnterpriseManageFragComponent;
import com.sgb.kjwl.di.manage.EnterpriseManageFragModule;
import com.sgb.kjwl.model.entity.manage.PromotionalAlbumEntity;
import com.sgb.kjwl.view.ui.adapter.manage.PromotionalAlbumAdapter;
import com.sgb.kjwl.viewmodel.manage.PromotionalAlbumForWebsiteModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;


/*
 * Author LiWei
 * Create Time 2021/4/15
 * Description 网站-宣传相册
 * */
public class PromotionalAlbumForWebsite extends AppLazyFragment {

    @Inject
    PromotionalAlbumForWebsiteModel viewModel;
    private FrPromotionalAlbumForWebsiteBinding binding;
    private PromotionalAlbumAdapter adapter;

    private String[] tabMenu = {"图片", "视频", "VR全景"};

    private String websiteNo;
    private int websiteType;
    private int mFileType = -1;
    private boolean isFirst = true;

    public static PromotionalAlbumForWebsite newInstance(String websiteNo, int websiteType) {
        PromotionalAlbumForWebsite fragment = new PromotionalAlbumForWebsite();
        Bundle args = new Bundle();
        args.putString("websiteNo", websiteNo);
        args.putInt("websiteType", websiteType);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        super.setupFragmentComponent(appComponent);
        DaggerEnterpriseManageFragComponent.builder().appComponent(appComponent)
                .enterpriseManageFragModule(new EnterpriseManageFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fr_promotional_album_for_website, container, false);
        if (getArguments() != null) {
            websiteNo = getArguments().getString("websiteNo");
            websiteType = getArguments().getInt("websiteType");
        }
        return binding.getRoot();
    }

    @Override
    protected void initView() {
        GridLayoutManager manager = new GridLayoutManager(getActivity(), 2);
        adapter = new PromotionalAlbumAdapter(getActivity(), null, manager, Glide.with(this));
        binding.listView.setLayoutManager(manager);
        binding.listView.setAdapter(adapter);
        binding.listView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.requestAlbumData(websiteNo, websiteType, false, mFileType);
            }

            @Override
            public void onLoadMore() {
                viewModel.requestAlbumData(websiteNo, websiteType, true, mFileType);
            }
        });
        binding.tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) { //第一次进来会执行
                if (!isFirst){
                    int tabCount = binding.tabLayout.getTabCount();
                    for (int i = 0; i < tabCount; i++) {
                        if (binding.tabLayout.getTabAt(i) != null && binding.tabLayout.getTabAt(i).getCustomView() != null
                                && binding.tabLayout.getTabAt(i).getCustomView().findViewById(R.id.tv_name) != null)
                            setTabStatus(i == tab.getPosition(), binding.tabLayout.getTabAt(i).getCustomView().findViewById(R.id.tv_name));
                    }
                    // TODO: 2021/8/19  加载数据
                    mFileType = tab.getPosition() + 1;
                    viewModel.requestAlbumData(websiteNo, websiteType, false, mFileType);
                } else {
                    isFirst = false;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        initCustomizeTab();
        mFileType = 1;//默认选图片
    }

    private void initCustomizeTab() {
        View mTabCustomerView;
        TabLayout.Tab mTab;
        for (int i = 0; i < tabMenu.length; i++) {
            mTabCustomerView = LayoutInflater.from(getContext()).inflate(R.layout.item_tablayout_customize_website_second_view, null);
            TextView tvName = mTabCustomerView.findViewById(R.id.tv_name);
            tvName.setText(tabMenu[i]);
            mTab = binding.tabLayout.newTab();
            mTab.setCustomView(mTabCustomerView);
            binding.tabLayout.addTab(mTab);
            setTabStatus(i == 0, tvName);
        }
    }

    /**
     *
     * @param isChoose 选择的tab
     * @param tvName tab样式
     */
    private void setTabStatus(boolean isChoose, TextView tvName) {
        tvName.setSelected(isChoose);
        tvName.setTextColor(getResources().getColor(isChoose ? R.color.color_main : R.color.color_tab_three));
    }

    @Override
    protected void onLazyLoad() {
        viewModel.requestAlbumData(websiteNo, websiteType, false, mFileType);
    }

    public void showList(List<PromotionalAlbumEntity> list, boolean isLoadMore, boolean isLast) {
        if (isLoadMore) {
            binding.listView.loadMoreComplete();
            if (list != null && list.size() > 0) {
                adapter.addDatas(list);
            }
            binding.listView.setNoMore(isLast);
        } else {
            binding.listView.refreshComplete();
            if (list != null && list.size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                adapter.setDatas(list);
            } else {
                adapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }
}
